var currentTool:String;
var loadInitCount:Number;

function init()
{
	mlf.mcmanager.MessageAgent.getMessageDispatcher().dispatch({type:"onCoverProgress", param:"Init "+this._name+" ..."});

	invokeInitComplete();
}

function invokeInitComplete()
{
	loadInitCount=0;
	Packages.system.mlf.GateKeeper.addListener("onNavigationEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onToolsEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onMessageBarEvent", this);
	var smcInstance=mlf.mcmanager.SMCManager.getInstance(this._name);
	smcInstance.addEventListener("onLoadInit", this);
	_root.NavigationBar.swapDepths(100);

	Packages.model.Context.messageBar=Packages.system.mlf.GateKeeper.scanElement(this._name, "tf_container");
}

function onLoadInit(evt)
{	
	loadInitCount++;
	if (loadInitCount==3)
	{
		Packages.components.SceneAgent.checkBasicViewCount();
		var smcInstance=mlf.mcmanager.SMCManager.getInstance(this._name);
		smcInstance.removeEventListener("onLoadInit", this);
		var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
		basic._visible=true;
		Packages.system.render.Transition.fadeType1(basic);
	}
	
	if (evt.value._name.indexOf("mouselayer")>-1)
	{
		Packages.system.events.ThemeMouseEvent.initialize(evt.value);
	}
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onNavigationEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onToolsEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onMessageBarEvent", this);
}

function onMessageBarEvent(evt:Object)
{
	if (evt.param=="onRollOver")
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("magicpen");
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("normal");
	}
	else if (evt.param=="onRollOut")
	{
		if (!evt.target.hitTest(_root._xmouse, _root._ymouse, true))
		{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("pen");
		}
		else Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("scale");
	}
	else if (evt.param=="onPress")
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("move");
	}
	else if (evt.param=="onRelease")
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("normal");
		messageDisplay(80);
	}
}

function messageDisplay(offset:Number)
{
	var messageContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "message_container");
	if (messageContainer._y!=520 && offset==80) return;
	else if (messageContainer._y!=600 && offset==-80) return;
	Packages.system.render.Animation.move(messageContainer, "_y", offset);
}

function onToolsEvent(evt:Object)
{
	// Packages.log("onToolsEvent."+evt.param, "debug");
	
	var toolsInstance=evt.target._parent;
	// Packages.log("toolsInstance: "+toolsInstance, "debug");
	if (evt.param=="onMainSpotRollOver")
	{
		
		// condition
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("magicpen");
		
		if (toolsInstance._x==119) Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("normal");
		else Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("move");
	}
	else if (evt.param=="onMainSpotRollOut")
	{	
		if (!evt.target.hitTest(_root._xmouse, _root._ymouse, true))
		{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("pen");
		}
	}
	else if (evt.param=="onMainSpotRelease")
	{
		// dispose the portlet
		// fly out
		// rotation to normal
		if (toolsInstance._x==70)
		{
			toolsInstance._x=119;
			toolsInstance._y=120;

			currentTool="normal";
			disposeTool();
			Packages.system.render.Transition.flyType3(toolsInstance, selectTools);
			Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("normal");
		}
		else return;
	}
	else if (evt.param=="onTitleRollOver")
	{
	Packages.system.events.ThemeMouseEvent.changeAttachedMouse("magicpen");
	Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("scale");
	}
	else if (evt.param=="onTitleRollOut")
	{
		if (!toolsInstance.mainSpot.hitTest(_root._xmouse, _root._ymouse, true))
		{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("pen");
		}
	}
	else if (evt.param=="onTitleRelease")
	{
		// fly in 
		// rotation
		// create new portlet
		// initial postion {x:119, y:120}

		var titleName=evt.target._name;
		var label:String;
		if (titleName=="title1")
		{
			label="searchbar";
		}
		else if (titleName=="title2")
		{
			label="player";
		}
		else if (titleName=="title3")
		{
			label="messenger";
		}
		else if (titleName=="title4")
		{
			label="helper";
		}
		if (currentTool==label) return;
		currentTool=label;

		if (toolsInstance._x==119)
		{
			toolsInstance._x=70;
			toolsInstance._y=180;
			Packages.system.render.Transition.flyType3(toolsInstance, selectTools);

		}
		else selectTools();
		
		// tmpShowMessage();
	}
}

function tmpShowMessage()
{
	/*
	Packages.modula.MessageCenter.pushMessage("system message: system message #1");
	Packages.modula.MessageCenter.pushMessage("system message: system message #2");
	Packages.modula.MessageCenter.pushMessage("system message: system message #3");
	Packages.modula.MessageCenter.pushMessage("system message: system message #4");
	Packages.modula.MessageCenter.pushMessage("system message: system message #5");
	var basic:MovieClip=Packages.system.mlf.GateKeeper.scanElement("NavigationBar", "basic");
	var sysmsg=Packages.system.mlf.GateKeeper.scanDictionary(basic, "SystemMessage");
	var cont1:String=Packages.system.utils.TextUtil.formatText("tfContent", sysmsg+": realtime message #1");
	Packages.modula.MessageCenter.pushMessage(cont1, 0);
	Packages.modula.MessageCenter.pushMessage(sysmsg+": realtime message #2", 0);
	*/
	var albumListContentContainer=Packages.system.mlf.GateKeeper.scanElement("BookSpecial", "albumListContentContainer");
	for (var a in albumListContentContainer)
	{
		if (albumListContentContainer[a] instanceof MovieClip)
		{
			Packages.log(a+": "+albumListContentContainer[a]._name+"|"+albumListContentContainer[a]._width+"|"+albumListContentContainer[a]._height, "debug");
			for (var b in albumListContentContainer[a])
			{
				if (albumListContentContainer[a][b] instanceof MovieClip)
				{
					Packages.log(b+": "+albumListContentContainer[a][b]._name+"|"+albumListContentContainer[a][b]._width+"|"+albumListContentContainer[a][b]._height, "debug");
				}
			}
		}
	}
}

function selectTools()
{
	// setClockTime not here!
	var tools:MovieClip=Packages.system.mlf.GateKeeper.scanElement("NavigationBar", "tools");
	Packages.system.mlf.GateKeeper.invoke(tools, "playToLabel", new Array(currentTool));
	Packages.system.mlf.GateKeeper.invoke(tools, "setClockTime", null);
	disposeTool();
	var path:String="app/tools/";
	if (currentTool=="searchbar")
	{
		path+="SearchEngine.xml";
	}
	else if (currentTool=="player")
	{
		path+="Player.xml";
	}
	else if (currentTool=="messenger")
	{
		path+="Messenger.xml";
	}
	else if (currentTool=="helper")
	{
		path+="Helper.xml";
	}
	Packages.system.mlf.GateKeeper.loadML2byPath(path);
}

function disposeTool()
{
	if (Packages.model.Context.activeTool!=null)
	{
		// Packages.system.mlf.GateKeeper.unloadML2bySlot(Packages.model.Context.activeTool._name);
		Packages.system.mlf.GateKeeper.removePopup(Packages.model.Context.activeTool._name);
		Packages.model.Context.activeTool["dispose"].apply(Packages.model.Context.activeTool, null);
	}
	Packages.model.Context.activeTool=null;
}

function onNavigationEvent(evt:Object)
{
	var navigationInstance=evt.target._parent;
	if (evt.param=="onMainSpotRollOver")
	{
		navigationInstance.play();
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("magicpen");
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("rotation");
	}
	else if (evt.param=="onMainSpotRollOut")
	{
		navigationInstance.stop();
		
		if (!navigationInstance.mainSpot.hitTest(_root._xmouse, _root._ymouse, true)) Packages.system.events.ThemeMouseEvent.changeAttachedMouse("pen");
	}
	else if (evt.param=="onMainSpotPress")
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("move");
	}
	else if (evt.param=="onMainSpotRelease")
	{		
		if (navigationInstance._x==0)
		{
			navigationInstance._x=250;
			navigationInstance._y=250;
			navigationInstance.logo._x=180;
			navigationInstance.logo._y=180;
			navigationInstance.logo._alpha=100;
		}
		else
		{
			navigationInstance._x=0;
			navigationInstance._y=0;
			navigationInstance.logo._x=260;
			navigationInstance.logo._y=260;
			navigationInstance.logo._alpha=50;
		}

		Packages.system.render.Transition.flyType1(navigationInstance);
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("rotation");
	}
	else if (evt.param=="onTitleRollOver")
	{
		navigationInstance.stop();
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("magicpen");
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("scale");
	}
	else if (evt.param=="onTitleRollOut")
	{
		if (!navigationInstance.mainSpot.hitTest(_root._xmouse, _root._ymouse, true))
		{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("pen");
		}
	}
	else if (evt.param=="onTitleRelease")
	{
		var titleName=evt.target._name;
		var path;
		// navigationInstance.gotoAndStop(titleName);
		if (titleName=="title1")
		{
			path="HomePage";
		}
		else if (titleName=="title2")
		{
			path="myReadjoy/PersonPage";
		}
		else if (titleName=="title3")
		{
			path="bookPage/BookList";
		}
		else if (titleName=="title4")
		{
			path="Friends";
		}
		else if (titleName=="title5")
		{
			path="groups/GroupList";
		}
		else if (titleName=="title6")
		{
			path="Market";
		}
		if (Packages.model.Context.loginFlag) Packages.components.SceneAgent.loadScene("content/"+path);
	}
}